package org.dizem.groupon.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import java.util.Map;

import static org.dizem.groupon.Constants.*;

/**
 * User: dizem@126.com
 * Time: 11-6-27 下午1:09
 */
public class AdminAuthorizationInterceptor extends AbstractInterceptor {

	private String ignoreActions;

	public String getIgnoreActions() {
		return ignoreActions;
	}

	public void setIgnoreActions(String ignoreActions) {
		this.ignoreActions = ignoreActions;
	}

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		Map session = actionInvocation.getInvocationContext().getSession();
		String type = (String) session.get(SESSION_USER_TYPE);
		String username = (String) session.get(SESSION_USERNAME);
		boolean ignore = false;

		String currentAction = actionInvocation.getProxy().getActionName();
		String[] actions = currentAction.split(",");

		for (String action : actions) {
			if (action.trim().equals(currentAction)) {
				ignore = true;
				break;
			}
		}

		if (username != null && type != null && type.equals(USER_TYPE.ADMIN.name()) || ignore)
			return actionInvocation.invoke();
		else
			return "error";
	}
}
